package com.bonc.rms.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.bonc.rms.service.LoginService;

@RestController
@RequestMapping(value = "/loginController")
public class LoginController {
    
    @Autowired
    LoginService loginService;
    
    /**
     * 用户登录
     * @param name
     * @param password
     * @param httpSession
     * @return
     */

    @RequestMapping(value = "/login")
    public ModelAndView login(@RequestParam(name = "userName", required = true) String userName,
                              @RequestParam(name = "password", required = true) String password,
                              HttpSession httpSession) {
        // 验证用户名和密码
        
        Map<String, Object> rsMap = loginService.loginValidate(userName, password);
        if((Integer)rsMap.get("tag") == 1){
            ModelAndView modelAndView = new ModelAndView("userPage/user");
            modelAndView.addObject("loginTag", "success");
            httpSession.setAttribute("userId", rsMap.get("userId"));
            httpSession.setAttribute("userName", rsMap.get("userName"));
            return modelAndView;
 
        }else{
            if((Integer)rsMap.get("tag") == -1);
            ModelAndView modelAndView = new ModelAndView("login/login");
            modelAndView.addObject("loginTag", "error");
            return modelAndView;
        }

    }
    
    /*
     * 管理员登录
     */
    @RequestMapping(value = "/mLogin")
	    public ModelAndView mLogin(@RequestParam(name = "userName", required = true) String userName,
	            @RequestParam(name = "password", required = true) String password,
	            HttpSession httpSession) {
	// 验证用户名和密码
	
	Map<String, Object> rsMap = loginService.mLogin(userName, password);
	if((Integer)rsMap.get("tag") == 1){
	ModelAndView modelAndView = new ModelAndView("managerPage/manageUser");
	modelAndView.addObject("loginTag", "success");
	httpSession.setAttribute("userId", rsMap.get("userId"));
	httpSession.setAttribute("userName", rsMap.get("userName"));
	return modelAndView;
	
	}else{
	if((Integer)rsMap.get("tag") == -1);
	ModelAndView modelAndView = new ModelAndView("login/login");
	modelAndView.addObject("loginTag", "error");
	return modelAndView;
	}
	
 }

}
